package com.lay.config;


import com.lay.shiro.CustomerRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    //配置过滤器
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager);
        //配置系统公共资源
        //配置系统受限资源
        Map<String,String> map = new HashMap<>();
        map.put("/","anon");
        map.put("/login1.do","anon");
        map.put("/towelcome.do","anon");
        map.put("/static/**","anon");
        map.put("/**","authc");
        //设置认证界面路径
        factoryBean.setUnauthorizedUrl("/");
        factoryBean.setFilterChainDefinitionMap(map);
        factoryBean.setUnauthorizedUrl("/");
        return factoryBean;
    }

    //创建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("getRealm") Realm realm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm);
        return securityManager;
    }
    //自定义Realm
    @Bean
    public Realm getRealm(){
        CustomerRealm realm = new CustomerRealm();
        return realm;
    }
}
